package com.erp.module.article

import android.content.Intent
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.PopupWindow
import android.widget.RelativeLayout
import androidx.appcompat.app.AlertDialog
import com.erp.R
import com.erp.activity.GonggaoTextActivity
import com.erp.bean.ArticleBean
import com.erp.myAdapter.ArticleAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_article.*
import kotlinx.android.synthetic.main.pop_address_picker.view.*

/**
 * Created by Administrator on 2019/3/30/030.
 * 作者：刘洋
 */
class FragmentArticle :BaseKFragment(),ArticleContract.ArticleView, PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, AdapterView.OnItemClickListener, View.OnClickListener {

    lateinit var list : MutableList<ArticleBean>
    lateinit var adapter: ArticleAdapter
    var updatedialog: AlertDialog? = null
    lateinit var P : IArticleP

    override fun getLayoutId(): Int {
        return R.layout.fragment_article
    }

    override fun initData() {
        list = ArrayList<ArticleBean>()
        P = IArticleP(list,this,context!!)
        adapter=ArticleAdapter(list,context!!)
        LvArticle.adapter=adapter
        LvArticle.onItemClickListener = this
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        Pull.setheader(true)
        Pull.headerRefreshing()
        P.getArticleCategory()
        BtGuolv.setOnClickListener(this)
        TvXuanZe.setOnClickListener(this)
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
    }

    fun qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    override fun xialaOk() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val intent = Intent(activity, GonggaoTextActivity::class.java)
        intent.putExtra("code", 100)
        intent.putExtra("content",list[position].content)
        intent.putExtra("title", "卡诺亚资料")
        startActivity(intent)
    }
    var category = ""
    override fun onClick(v: View?) {
        when(v){
            BtGuolv -> {
                val acbean = ArticleBean.ArticleSouSuo()
                acbean.title=EtSouSuo.text.toString()
                acbean.starttime=TvStartTime.text.toString()
                acbean.endtime=TvEndTime.text.toString()
                acbean.category=this.category
                P.sousuo(acbean)
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
            }
            TvStartTime->{
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime.text.toString(), activity)
            }
            TvEndTime->{
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择结束时间", TvEndTime.text.toString(), activity)
            }
            TvXuanZe -> {
                val popupWindow = PopupWindow(context)
                val rootView = LayoutInflater.from(activity).inflate(R.layout.pop_address_picker, null, false)
                rootView.apvAddress.setOnAddressPickerSure { address, category, category1, category2 ->
                    TvXuanZe.text = address
                    if(category2!=null){
                        this.category=category2
                    }else if(category1!=null){
                        this.category=category1
                    }else if(category!=null){
                        this.category=category
                    }
                    popupWindow.dismiss()
                }
                rootView.apvAddress.defaultProvince="一级导航"
                rootView.apvAddress.defaultCity="二级导航"
                rootView.apvAddress.defaultDistrict="三级导航"
                rootView.apvAddress.categoryData(fenzu);
                popupWindow.contentView = rootView
                popupWindow.isFocusable = true
                popupWindow.isOutsideTouchable = true
                val params = rootView.apvAddress.layoutParams as RelativeLayout.LayoutParams
                params.height = sp.getInt("height",0)/2//设置当前控件布局的高度
                rootView.apvAddress.layoutParams = params//将设置好的布局参数应用到控件中
                popupWindow.animationStyle = R.style.showPopupAnimation
                popupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
                popupWindow.height = sp.getInt("height",0)*5/10
                popupWindow.showAtLocation(TvXuanZe, Gravity.BOTTOM, 0, 0)
            }
        }
    }

    var fenzu = ""
    override fun setSpSuperiorSection(data : String) {
        fenzu=data
//        listC1=list
//        val adapter = ArrayAdapter(activity, R.layout.lv_sp_tvitem, listC1)
//        SpSuperiorSection.adapter = adapter
    }


}